(function(){
	var _modules={},
	scripts = document.getElementsByTagName('script'),
	len = scripts.length,
	currentScript = scripts[len-1];

	function define(id,fn,update){
		if(id==="")
			return;
		if(_modules[id]&&!update)
			throw"Module "+id+" already exist!";
		if(typeof fn === "function"){
			_modules[id] = {id:id,fn:fn,exports:{}}
		}else{
			_modules[id]={exports:fn}}
	}
	
	function require(id){
		var mod=_modules[id],fn,ret;
		if(mod){
			fn=mod.fn;
			if(fn){
				ret=typeof fn==="function"?fn(require,mod.exports,mod):fn;
				if(ret)
					mod.exports=ret;
				delete mod.fn
			}
			return mod.exports
		}else{
			throw"Module "+id+" not exist!"
		}
	} 
	window.mt = {};
	mt.module =function(){
		if(arguments.length>=2){
			return define.apply(define,arguments)
		}else{
			return require.apply(require,arguments)
		}
	}

	function initPageScirpt(currentScript,init){
		init = init.split(',');
		if(init.length){

			var complete = function(event){
				var event = event || window.event;
                var _event = event.srcElement || event.target;
                _event.setAttribute('init-status','1');
				for(var i=0;i<init.length;i++){
					mt.module(init[i]);
				}
			}

			currentScript.onload = function(event){
				complete(event);
			};

			//script.onerror = onerror;
			currentScript.onreadystatechange = function () {
				var state = this.readyState;
				if (state === 'loaded' || state === 'complete') {
					currentScript.onreadystatechange = _blankFn;
					complete();
				}
			}
		}
	}
	
	if(scripts.length > 1){
		currentScript = function(){
			var _currentScript = [];
			
			for(var i = scripts.length - 1 ; i >=0 ; i--){
				var init = scripts[i].getAttribute('init');
				var initStatus = scripts[i].getAttribute('init-status');
				if(init && !initStatus){
					_currentScript.push(scripts[i]);
				}
			}

			return _currentScript;
		}();
	}

	var 
		_blankFn = function(){};
	for(var i=0;i<currentScript.length;i++){
		var init = currentScript[i].getAttribute('init');
		initPageScirpt(currentScript[i],init);
	}
	
})()